package com.settlement.system.model.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.settlement.system.common.base.BaseEntity;
import com.settlement.system.common.enums.AuditManagerEnum;
import com.settlement.system.common.enums.CancellationEnum;
import com.settlement.system.common.enums.CancellationStatusEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 标准核销表
 * </p>
 *
 * @author huangrensen
 * @since 2023-08-05
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SysAudit extends BaseEntity {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id")
    private String id;

    // 所属角色
    private String belongRole;

    // 所属平台
    private String platform;

    // 关联单号
    private String linkNo;

    // 账单月份
    private String billDate;

    // 发退货时间
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime sendReturnTime;

    // 下单时间
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime orderTime;

    // 对账系统平台编码
    private String secordSourceCode;

    // 对账系统平台名称
    private String secordSourceName;

    // 电商平台店铺编码
    private String thirdSourceCode;

    // 电商平台店铺名称
    private String thirdSourceName;

    // 对账平台店铺编码
    private String dzPlatformStoreCode;

    // 自有平台主订单号
    private String onwerPreformNo;

    // 原始主订单号
    private String sourcePreformMainNo;

    // 原始子订单号
    private String sourcePreformSubNo;

    // PO单号
    private String poNo;

    // 渠道编码
    private String channelCode;

    // 渠道
    private String channel;

    // 内部系统电商平台编码
    private String innerShopCode;

    // 内部系统电商平台名称
    private String innerShopName;

    // 内部系统店铺编码
    private String shopCode;

    // 内部系统店铺名称
    private String shopName;

    // 入账主体
    private String receiveCompany;

    // 电商平台登录账号
    private String shopAccount;

    // 财务系统店铺编码
    private String billSysStore;

    // 财务系统店铺名称
    private String billSysStoreName;

    // 商品单价
    private BigDecimal price = BigDecimal.ZERO;

    // 发货数量
    private Integer qty = 0;

    // 发货金额
    private BigDecimal sendAmount = BigDecimal.ZERO;

    // 退货数量
    private Integer returnQty = 0;

    // 退货金额
    private BigDecimal returnAmount = BigDecimal.ZERO;

    // 收款日期
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime receiveDate;

    // 收款金额
    private BigDecimal receiveAmount = BigDecimal.ZERO;

    // 退款日期
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime refundDate;

    // 退款金额
    private BigDecimal refundAmount = BigDecimal.ZERO;

    // 保证金退款金额
    private BigDecimal depositAmount = BigDecimal.ZERO;

    // 自系统调账日期
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime systemReconciliationDate;

    // 自系统调账数量
    private Integer systemReconciliationQty = 0;

    // 自系统调账金额
    private BigDecimal systemReconciliationAmount = BigDecimal.ZERO;

    // 财务调账日期
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime reconciliationDate;

    // 财务调账数量
    private Integer reconciliationQty = 0;

    // 财务调账金额
    private BigDecimal reconciliationAmount = BigDecimal.ZERO;

    // 差异数量
    private Integer differenceQty = 0;

    // 应收账款余额
    private BigDecimal receiveBillAmount = BigDecimal.ZERO;

    //业务日期
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime businessDate;

    // 是否核消 是 或 否
    private String audit = CancellationEnum.YES.getValue();

    // 编号编码
    private String ruleCode;
    // 规则名称
    private String ruleName;

    // 核销结果分类
    private String mainTypeName;

    // 订单核消细类名称
    private String auditDetailName;

    // 异常售后未提报金额
    private BigDecimal serviceBillAmount = BigDecimal.ZERO;

    // 自认损失金额
    private BigDecimal ownerLossAmount = BigDecimal.ZERO;

    // 是否推送
    private String push;

    // 角色负责人待办标识(0:否， 1：是)
    private Integer manager = AuditManagerEnum.NO.getValue();

    // 核消状态
    private String auditStatus;

    // 单据编号
    private String orderNo;

    // 店铺id
    private Long storeId;

    // 结算前退款
    private BigDecimal refundsBeforeCheckout = BigDecimal.ZERO;

    // 结算后退款
    private BigDecimal refundsAfterCheckout = BigDecimal.ZERO;


    // 创建人ID
    private Long createBy;

    // 修改人ID
    private Long updateBy;

    @Version
    @TableField(fill = FieldFill.INSERT)
    private Integer version;

    // 检查是否有关联账单
    @TableField(exist = false)
    private boolean hasRelationBill;

    public static List<String> getSumColumn() {
        List<String> columnList = new ArrayList<>();
        columnList.add("qty");
        columnList.add("sendAmount");
        columnList.add("returnQty");
        columnList.add("returnAmount");
        columnList.add("receiveAmount");
        columnList.add("refundAmount");
        columnList.add("depositAmount");
        return columnList;
    }

    // 计算应收账款余额
    public void sumReceiveBillAmount() {
        // 应收账款余额 = （发货金额+自系统调账金额+退货金额+财务调账金额）-（收款金额+退款金额+保证金退款）- 自认损失金额
        BigDecimal zero = BigDecimal.ZERO;
        if (this.sendAmount != null) {
            zero = zero.add(this.sendAmount);
        }
        if (this.returnAmount != null) {
            zero = zero.add(this.returnAmount);
        }
        if (this.systemReconciliationAmount != null) {
            zero = zero.add(this.systemReconciliationAmount);
        }
        if (this.reconciliationAmount != null) {
            zero = zero.add(this.reconciliationAmount);
        }
        if (this.receiveAmount != null) {
            zero = zero.subtract(this.receiveAmount);
        }
        if (this.refundAmount != null) {
            zero = zero.subtract(this.refundAmount);
        }
        if (this.depositAmount != null) {
            zero = zero.subtract(this.depositAmount);
        }
        if (this.ownerLossAmount != null) {
            zero = zero.subtract(this.ownerLossAmount);
        }
        this.receiveBillAmount = zero;
    }
}
